package com.qywx.controller;

import com.qywx.service.PhotoService;
import com.qywx.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @ClassName PhotoController
 * @Description: TODO
 * @Author wmj
 * @Date 2020/3/16
 * @Version V1.0
 **/
@Api(value = "图片上传接口", tags = {"图片上传与回显接口"})
@RestController
@RequestMapping("/photo")
public class PhotoController {

    @Autowired
    PhotoService photoService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传图片", notes = "")
    public R saveImage(@RequestParam("file") MultipartFile file){
        return photoService.saveImage(file);
    }

    @GetMapping("/read")
    @ApiOperation(value = "获取图片", notes = "")
    public void readImage(@RequestParam("id") int id, HttpServletResponse response) throws IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //response.setHeader()  设置响应报头，在这里设置为不可缓存
        byte[] img = null;//通过一个byte[]来接收图片
        img=photoService.redImg(id);
        //OutputStream 以流的形式将图片信息返回给页面
        OutputStream outputStream = response.getOutputStream();
        outputStream.write(img);
        outputStream.flush();//排出流中所有数据
        outputStream.close();//关闭流

    }

   /* @PostMapping("/up")
    public R upload(@RequestParam("file") MultipartFile file) {
        String filename = FileUpload.writeUploadFile(file,"photo");
        return R.ok(filename);
    }*/

}
